<?php
/**
* @file $Id: Widgets.fnc.php 528 2007-05-21 19:55:02Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function Widgets($item)
{	global $extra,$_FOCUS;

	if(!is_array($extra['functions']))
		$extra['functions'] = array();

	if(User('PROFILE')!='parent' && User('PROFILE')!='student')
	{
		switch($item)
		{
			case 'all':
				$extra['search'] .= '<A onclick="switchMenu(\'scheduling_table\');"><IMG SRC=assets/arrow_right.gif id=scheduling_table_arrow> <B>'._('Scheduling').'</B></A><BR><TABLE class=EntireBox width=100% id=scheduling_table style="display:none;">';
				Widgets('course');
				Widgets('request');
				$extra['search'] .= '</TABLE><A onclick="switchMenu(\'absences_table\');"><IMG SRC=assets/arrow_right.gif id=absences_table_arrow> <B>'._('Absences').'</B></A><BR><TABLE class=EntireBox width=100% id=absences_table style="display:none;">';
				Widgets('absences');
				$extra['search'] .= '</TABLE><A onclick="switchMenu(\'grades_table\');"><IMG SRC=assets/arrow_right.gif id=grades_table_arrow> <B>'._('Grades').'</B></A><BR><TABLE class=EntireBox width=100% cellpadding=5 id=grades_table style="display:none;">';
				Widgets('gpa');
				Widgets('average_grade');
				Widgets('class_rank');
				Widgets('letter_grade');
				$extra['search'] .= '</TABLE><A onclick="switchMenu(\'eligibility_table\');"><IMG SRC=assets/arrow_right.gif id=eligibility_table_arrow> <B>'._('Eligibility').'</B></A><BR><TABLE class=EntireBox width=100% id=eligibility_table style="display:none;">';
				Widgets('eligibility');
				Widgets('activity');
				$extra['search'] .= '</TABLE><A onclick="switchMenu(\'discipline_table\');"><IMG SRC=assets/arrow_right.gif id=discipline_table_arrow> <B>'._('Discipline').'</B></A><BR><TABLE class=EntireBox width=100% id=discipline_table style="display:none;">';
				Widgets('discipline');
				$extra['search'] .= '</TABLE><A onclick="switchMenu(\'billing_table\');"><IMG SRC=assets/arrow_right.gif id=billing_table_arrow> <B>'._('Billing').'</B></A><BR><TABLE class=EntireBox width=100% id=billing_table style="display:none;">';
				Widgets('balance');
				$extra['search'] .= '</TABLE>';
			break;

			case 'course':
				if($_REQUEST['w_course_period_id'])
				{
					if($_REQUEST['w_course_period_id_which']=='course')
					{
						$course = DBGet(DBQuery("SELECT c.TITLE AS COURSE_TITLE,cp.TITLE,cp.COURSE_ID,cp.COURSE_WEIGHT FROM COURSE_PERIODS cp,COURSES c WHERE c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_PERIOD_ID='".$_REQUEST['w_course_period_id']."'"));
						$extra['FROM'] .= ",SCHEDULE w_ss";
						$extra['WHERE'] .= " AND w_ss.STUDENT_ID=s.STUDENT_ID AND w_ss.SYEAR=ssm.SYEAR AND w_ss.SCHOOL_ID=ssm.SCHOOL_ID AND w_ss.COURSE_ID='".$course[1]['COURSE_ID']."' AND w_ss.COURSE_WEIGHT='".$course[1]['COURSE_WEIGHT']."' AND ('".DBDate()."' BETWEEN w_ss.START_DATE AND w_ss.END_DATE OR w_ss.END_DATE IS NULL)";
						$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Course').': </b></font>'.$course[1]['COURSE_TITLE'].'<BR>';
					}
					else
					{
						$extra['FROM'] .= ",SCHEDULE w_ss";
						$extra['WHERE'] .= " AND w_ss.STUDENT_ID=s.STUDENT_ID AND w_ss.SYEAR=ssm.SYEAR AND w_ss.SCHOOL_ID=ssm.SCHOOL_ID AND w_ss.COURSE_PERIOD_ID='".$_REQUEST['w_course_period_id']."' AND ('".DBDate()."' BETWEEN w_ss.START_DATE AND w_ss.END_DATE OR w_ss.END_DATE IS NULL)";
						$course = DBGet(DBQuery("SELECT c.TITLE AS COURSE_TITLE,cp.TITLE,cp.COURSE_ID,cp.COURSE_WEIGHT FROM COURSE_PERIODS cp,COURSES c WHERE c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_PERIOD_ID='".$_REQUEST['w_course_period_id']."'"));
						$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Section').': </b></font>'.$course[1]['COURSE_TITLE'].': '.$course[1]['TITLE'].'<BR>';
					}
				}
				if(AllowUse('Scheduling/Schedule.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Course')."</TD><TD><DIV id=\"course_div\"></DIV> <A HREF=# onclick=\"window.open('Modules.php?modname=misc/ChooseCourse.php','','scrollbars=yes,resizable=yes,width=800,height=400');\"><SMALL>"._('Choose')."</SMALL></A></TD></TR>";
			break;
	
			case 'request':
				// PART OF THIS IS DUPLICATED IN PrintRequests.php
				if($_REQUEST['request_course_id'] && $_REQUEST['request_course_weight'])
				{
					$course = DBGet(DBQuery("SELECT c.TITLE FROM COURSES c WHERE c.COURSE_ID='".$_REQUEST['request_course_id']."'"));
					if(!$_REQUEST['not_request_course'])
					{
						$extra['FROM'] .= ",SCHEDULE_REQUESTS sr";
						$extra['WHERE'] .= " AND sr.STUDENT_ID=s.STUDENT_ID AND sr.SYEAR=ssm.SYEAR AND sr.SCHOOL_ID=ssm.SCHOOL_ID AND sr.COURSE_ID='".$_REQUEST['request_course_id']."' AND sr.COURSE_WEIGHT='".$_REQUEST['request_course_weight']."'";
						$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Request').': </b></font>'.$course[1]['TITLE'].' - '.$_REQUEST['request_course_weight'].'<BR>';
					}
					else
					{
						$extra['WHERE'] .= " AND NOT EXISTS (SELECT '' FROM SCHEDULE_REQUESTS sr WHERE sr.STUDENT_ID=ssm.STUDENT_ID AND sr.SYEAR=ssm.SYEAR AND sr.COURSE_ID='".$_REQUEST['request_course_id']."' AND sr.COURSE_WEIGHT='".$_REQUEST['request_course_weight']."') ";
						$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Missing Request').': </b></font>'.$course[1]['TITLE'].' - '.$_REQUEST['request_course_weight'].'<BR>';
					}
				}
				if(AllowUse('Scheduling/Requests.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Request')."</TD><TD><DIV id=\"weight_div\"></DIV> <A HREF=# onclick='window.open(\"Modules.php?modname=misc/ChooseRequest.php\",\"\",\"scrollbars=yes,resizable=yes,width=800,height=400\");'><SMALL>"._('Choose')."</SMALL></A></TD></TR>";
			break;
	
			case 'absences':
				if(is_numeric($_REQUEST['absences_low']) && is_numeric($_REQUEST['absences_high']))
				{
					if($_REQUEST['absences_low'] > $_REQUEST['absences_high'])
					{
						$temp = $_REQUEST['absences_high'];
						$_REQUEST['absences_high'] = $_REQUEST['absences_low'];
						$_REQUEST['absences_low'] = $temp;
					}
		
					if($_REQUEST['absences_term']!='FY')
						$mp_extra = " AND ad.MARKING_PERIOD_ID IN (".GetChildrenMP($_REQUEST['absences_term'],UserMP()).")";
					if($_REQUEST['absences_low']==$_REQUEST['absences_high'])
						$extra['WHERE'] .= " AND (SELECT COALESCE((SUM(STATE_VALUE-1)*-1),0.0) AS STATE_VALUE FROM ATTENDANCE_DAY ad WHERE ssm.STUDENT_ID=ad.STUDENT_ID AND ad.SYEAR=ssm.SYEAR ".$mp_extra.") = '$_REQUEST[absences_low]'";
					else
						$extra['WHERE'] .= " AND (SELECT COALESCE((SUM(STATE_VALUE-1)*-1),0.0) AS STATE_VALUE FROM ATTENDANCE_DAY ad WHERE ssm.STUDENT_ID=ad.STUDENT_ID AND ad.SYEAR=ssm.SYEAR ".$mp_extra.") BETWEEN '$_REQUEST[absences_low]' AND '$_REQUEST[absences_high]'";
					switch($_REQUEST['absences_term'])
					{
						case 'FY':
							$term = _('this school year to date');
						break;
						case 'SEM':
							$term = _('this semester to date');
						break;
						case 'QTR':
							$term = _('this marking period to date');
						break;
					}
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Days Absent ').$term._(' between').': </b></font>'.$_REQUEST['absences_low'].' &amp; '.$_REQUEST['absences_high'].'<BR>';
				}
				if(AllowUse('Attendance/StudentSummary.php') || AllowUse('Attendance/DailySummary.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Days Absent').'<BR><INPUT type=radio name=absences_term value=FY checked><small>'._('YTD')."</small><INPUT type=radio name=absences_term value=SEM><small>".GetMP(GetParentMP('SEM',UserMP()),'SHORT_NAME')."</small><INPUT type=radio name=absences_term value=QTR><small>".GetMP(UserMP(),'SHORT_NAME')."</small></TD><TD><small>"._('Between')." </small> <INPUT type=text name=absences_low size=3 maxlength=5> <small>&amp;</small> <INPUT type=text name=absences_high size=3 maxlength=5></TD></TR>";
			break;
			
			// added by oindrila.mukherjee@intel.com
			case 'average_grade':
				if(is_numeric($_REQUEST['avg_grade_low']) && is_numeric($_REQUEST['avg_grade_high']))
				{
					if($_REQUEST['avg_grade_low'] > $_REQUEST['avg_grade_high'])
					{
						$temp = $_REQUEST['avg_grade_high'];
						$_REQUEST['avg_grade_high'] = $_REQUEST['avg_grade_low'];
						$_REQUEST['avg_grade_low'] = $temp;
					}
			
					$extra['WHERE'] = " AND (SELECT AVG(srcg.PERCENT) FROM STUDENT_REPORT_CARD_GRADES srcg WHERE srcg.STUDENT_ID=ssm.STUDENT_ID AND srcg.MARKING_PERIOD_ID='$_REQUEST[avg_grade_term]') BETWEEN '$_REQUEST[avg_grade_low]' AND '$_REQUEST[avg_grade_high]' ";
			
					$_FOCUS['SearchTerms'] .= '<font color=gray><b> '._('Average Grade').' '.GetMP($_REQUEST['avg_grade_term']).' '._('between:').' </b></font>'.$_REQUEST['avg_grade_low'].' &amp; '.$_REQUEST['avg_grade_high'].'<BR>';
				}

				if(AllowUse('Grades/StudentRCGrades.php') || AllowUse('Grades/ReportCards.php'))
				{
					$qtr = UserMP();
					$sem = GetParentMP('SEM',UserMP());
					$fy = GetParentMP('FY',$sem);

					if(GetMP($fy,'POST_START_DATE') || GetMP($sem,'POST_START_DATE') || GetMP($qtr,'POST_START_DATE'))
					{
						$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Average Percent Grade')."<BR>";
						$selected = false;
						if(GetMP($fy,'POST_START_DATE'))
						{
							$extra['search'] .= '<INPUT type=radio name=avg_grade_term value='.$fy.' CHECKED><small>'.GetMP($fy,'SHORT_NAME').'</small>';
							$selected = true;
						}
						if(GetMP($sem,'POST_START_DATE'))
						{
							$extra['search'] .= '<INPUT type=radio name=avg_grade_term value='.$sem.(!$selected?' CHECKED':'').'><small>'.GetMP($sem,'SHORT_NAME').'</small>';
							$selected = true;
						}
						if(GetMP($qtr,'POST_START_DATE'))
						{
							$extra['search'] .= '<INPUT type=radio name=avg_grade_term value='.$qtr.(!$selected?' CHECKED':'').'><small>'.GetMP($qtr,'SHORT_NAME').'</small>';
							$selected = true;
						}

						$extra['search'] .= "</TD><TD><small>"._('Between')." </small> <INPUT type=text name=avg_grade_low size=3 maxlength=5> <small>&amp;</small> <INPUT type=text name=avg_grade_high size=3 maxlength=5></TD></TR>";
					}
				}
			break;
	
			case 'gpa':
				if(is_numeric($_REQUEST['gpa_low']) && is_numeric($_REQUEST['gpa_high']))
				{
					if($_REQUEST['gpa_low'] > $_REQUEST['gpa_high'])
					{
						$temp = $_REQUEST['gpa_high'];
						$_REQUEST['gpa_high'] = $_REQUEST['gpa_low'];
						$_REQUEST['gpa_low'] = $temp;
					}
					if($_REQUEST['list_gpa'])
					{
						$extra['SELECT'] .= ',sgc.CUMULATIVE_WEIGHTED_GPA,sgc.CUMULATIVE_GPA';
						$extra['columns_after']['WEIGHTED_GPA'] = _('Weighted GPA');
						$extra['columns_after']['GPA'] = _('GPA');
					}
					if(strpos($extra['FROM'],'STUDENT_GPA_CALCULATED sgc')===false)
					{
						$extra['FROM'] .= ",STUDENT_GPA_CALCULATED sgc";
						$extra['WHERE'] .= " AND sgc.STUDENT_ID=s.STUDENT_ID AND sgc.MARKING_PERIOD_ID='".$_REQUEST['gpa_term']."'";
					}
					$extra['WHERE'] .= " AND sgc.".(($_REQUEST['weighted']=='Y')?'WEIGHTED_':'')."GPA BETWEEN '$_REQUEST[gpa_low]' AND '$_REQUEST[gpa_high]' AND sgc.MARKING_PERIOD_ID='".$_REQUEST['gpa_term']."'";
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'.(($_REQUEST['gpa_weighted']=='Y')?_('Weighted').' ':'')._('GPA between:').' </b></font>'.$_REQUEST['gpa_low'].' &amp; '.$_REQUEST['gpa_high'].'<BR>';
				}
				if(AllowUse('Grades/StudentRCGrades.php') || AllowUse('Grades/ReportCards.php') || AllowUse('Grades/Transcripts.php'))
				{
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('GPA');
					if(SystemPreferences('WEIGHTED_GPA')=='Y')
						$extra['search'] .= "<BR><INPUT type=checkbox name=gpa_weighted value=Y><small>"._('Weighted')."</small>";
					$extra['search'] .= "<BR><INPUT type=radio name=gpa_term value=".GetParentMP('SEM',UserMP())." CHECKED><small>".GetMP(GetParentMP('SEM',UserMP()),'SHORT_NAME')."</small><INPUT type=radio name=gpa_term value=".UserMP()."><small>".GetMP(UserMP(),'SHORT_NAME')."</small></TD><TD><small>"._('Between')." </small><INPUT type=text name=gpa_low size=3 maxlength=5> <small>&amp;</small> <INPUT type=text name=gpa_high size=3 maxlength=5></TD></TR>";
				}
			break;
	
			case 'class_rank':
				if(is_numeric($_REQUEST['class_rank_low']) && is_numeric($_REQUEST['class_rank_high']))
				{
					if($_REQUEST['class_rank_low'] > $_REQUEST['class_rank_high'])
					{
						$temp = $_REQUEST['class_rank_high'];
						$_REQUEST['class_rank_high'] = $_REQUEST['class_rank_low'];
						$_REQUEST['class_rank_low'] = $temp;
					}
					if(strpos($extra['FROM'],'STUDENT_GPA_CALCULATED sgc')===false)
					{
						$extra['FROM'] .= ",STUDENT_GPA_CALCULATED sgc";
						$extra['WHERE'] .= " AND sgc.STUDENT_ID=s.STUDENT_ID AND sgc.MARKING_PERIOD_ID='".$_REQUEST['class_rank_term']."'";
					}
					$extra['WHERE'] .= " AND sgc.CLASS_RANK BETWEEN '$_REQUEST[class_rank_low]' AND '$_REQUEST[class_rank_high]'";
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Class Rank between:').' </b></font>'.$_REQUEST['class_rank_low'].' &amp; '.$_REQUEST['class_rank_high'].'<BR>';
				}
				if(AllowUse('Grades/StudentRCGrades.php') || AllowUse('Grades/ReportCards.php') || AllowUse('Grades/Transcripts.php'))
				{
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Class Rank')."<BR><INPUT type=radio name=class_rank_term value=".GetParentMP('SEM',UserMP())." CHECKED><small>".GetMP(GetParentMP('SEM',UserMP()),'SHORT_NAME')."</small><INPUT type=radio name=class_rank_term value=".UserMP()."><small>".GetMP(UserMP(),'SHORT_NAME')."</small>";
					if(strlen($pros = GetChildrenMP('PRO',UserMP())))
					{
						$pros = explode(',',str_replace("'",'',$pros));
						foreach($pros as $pro)
							$extra['search'] .= "<INPUT type=radio name=class_rank_term value=".$pro."><small>".GetMP($pro,'SHORT_NAME')."</small>";
					}
					$extra['search'] .= "</TD><TD><small>"._('Between')." </small><INPUT type=text name=class_rank_low size=3 maxlength=5> <small>&amp;</small> <INPUT type=text name=class_rank_high size=3 maxlength=5></TD></TR>";
				}
			break;
			
			case 'letter_grade':
				if(count($_REQUEST['letter_grade']))
				{
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'.($_REQUEST['letter_grade_exclude']=='Y'?'Without':'With').' '._('Report Card Grade:').' </b></font>';
					foreach($_REQUEST['letter_grade'] as $grade=>$Y)
					{
						$letter_grades .= ",'$grade'";
						$_FOCUS['SearchTerms'] .= $grade.', ';
					}
					$_FOCUS['SearchTerms'] = substr($_FOCUS['SearchTerms'],0,-2);
					
					if($_REQUEST['letter_grade_exclude']!='Y')
						$_FOCUS['SearchTerms'] .= ' &nbsp; <FORM action='.PreparePHP_SELF(array(),array('letter_grade_show_courses')).' method=POST><INPUT type=checkbox name=letter_grade_show_courses'.($_REQUEST['letter_grade_show_courses']=='Y'?' CHECKED':'').' value=Y onclick="this.form.submit();"> '._('Show Courses').'</FORM>';
					if($_REQUEST['letter_grade_show_courses'])
					{
						$extra['SELECT'] .= ',srcg_lg.COURSE_TITLE,srcg_lg.GRADE_TITLE';
						$extra['columns_before']['COURSE_TITLE'] = _('Course');
						$extra['columns_before']['GRADE_TITLE'] = _('Grade');
						$extra['FROM'] .= ',STUDENT_REPORT_CARD_GRADES srcg_lg ';
						$extra['WHERE'] .= " AND srcg_lg.STUDENT_ID=ssm.STUDENT_ID AND srcg_lg.SYEAR=ssm.SYEAR AND srcg_lg.GRADE_TITLE IN (".substr($letter_grades,1).") AND srcg_lg.MARKING_PERIOD_ID='".$_REQUEST['letter_grade_term']."' ";
						$extra['singular'] = _('Course');
						$extra['plural'] = _('Courses');
					}
					else
						$extra['WHERE'] .= " AND ".($_REQUEST['letter_grade_exclude']=='Y'?'NOT ':'')."EXISTS (SELECT '' FROM STUDENT_REPORT_CARD_GRADES sg3 WHERE sg3.STUDENT_ID=ssm.STUDENT_ID AND sg3.SYEAR=ssm.SYEAR AND sg3.GRADE_TITLE IN (".substr($letter_grades,1).") AND sg3.MARKING_PERIOD_ID='".$_REQUEST['letter_grade_term']."' )";
					$_FOCUS['SearchTerms'] .= '<BR>';
				}
				if(AllowUse('Grades/StudentRCGrades.php') || AllowUse('Grades/ReportCards.php') || AllowUse('Grades/Transcripts.php'))
				{
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Letter Grade')."<BR><INPUT type=checkbox name=letter_grade_exclude value=Y><small>"._('Did not receive')."</small><BR><INPUT type=radio name=letter_grade_term value=".GetParentMP('SEM',UserMP())." CHECKED><small>".GetMP(GetParentMP('SEM',UserMP()),'SHORT_NAME')."</small><INPUT type=radio name=letter_grade_term value=".UserMP()."><small>".GetMP(UserMP(),'SHORT_NAME')."</small>";
					if(strlen($pros = GetChildrenMP('PRO',UserMP())))
					{	
						$pros = explode(',',str_replace("'",'',$pros));
						foreach($pros as $pro)
							$extra['search'] .= "<INPUT type=radio name=letter_grade_term value=".$pro."><small>".GetMP($pro,'SHORT_NAME')."</small>";
					}
					$extra['search'] .= "</TD><TD>";
					if($_REQUEST['search_modfunc']=='search' || !$_REQUEST['search_modfunc'] || $_FOCUS['modules_search'])
						$letter_grades_RET = DBGet(DBQuery("SELECT TITLE FROM REPORT_CARD_GRADES WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."' ORDER BY SORT_ORDER"));
					$i = 0;
					if(count($letter_grades_RET))
					{
						foreach($letter_grades_RET as $grade)
						{
							if($used_grades[$grade['TITLE']])
								continue;

							if($i%9==0)
								$extra['search'] .= '<BR>';
						
							$extra['search'] .= '<INPUT type=checkbox value=Y name=letter_grade['.$grade['TITLE'].']>'.$grade['TITLE'];
							$i++;
							$used_grades[$grade['TITLE']] = true;
						}
					}
					$extra['search'] .= '</TD></TR>';
				}
			break;
	
			case 'eligibility':
				if($_REQUEST['ineligible']=='Y')
				{
					$start_end_RET = DBGet(DBQuery("SELECT TITLE,VALUE FROM PROGRAM_CONFIG WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' AND PROGRAM='eligibility' AND TITLE IN ('START_DAY','END_DAY')"));
					if(count($start_end_RET))
					{
						foreach($start_end_RET as $value)
							$$value['TITLE'] = $value['VALUE'];
					}
					
					switch(date('D'))
					{
						case 'Mon':
						$today = 1;
						break;
						case 'Tue':
						$today = 2;
						break;
						case 'Wed':
						$today = 3;
						break;
						case 'Thu':
						$today = 4;
						break;
						case 'Fri':
						$today = 5;
						break;
						case 'Sat':
						$today = 6;
						break;
						case 'Sun':
						$today = 7;
						break;
					}
	
					$start_date = strtoupper(date('d-M-y',time() - ($today-$START_DAY)*60*60*24));
					$end_date = strtoupper(date('d-M-y',time()));
					$extra['WHERE'] .= " AND (SELECT count(*) FROM ELIGIBILITY e WHERE ssm.STUDENT_ID=e.STUDENT_ID AND e.SYEAR=ssm.SYEAR AND e.SCHOOL_DATE BETWEEN '$start_date' AND '$end_date' AND e.ELIGIBILITY_CODE='FAILING') > '0'";
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Eligibility:').' </b></font>'._('Ineligible').'<BR>';
				}
				if(AllowUse('Eligibility/Student.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Ineligible')."</TD><TD><INPUT type=checkbox name=ineligible value='Y'></TD></TR>";
			break;
			
			case 'activity':
				if($_REQUEST['activity_id'])
				{
					$extra['FROM'] .= ",STUDENT_ELIGIBILITY_ACTIVITIES sea";
					$extra['WHERE'] .= " AND sea.STUDENT_ID=s.STUDENT_ID AND sea.SYEAR=ssm.SYEAR AND sea.ACTIVITY_ID='".$_REQUEST['activity_id']."'";
					$activity = DBGet(DBQuery("SELECT TITLE FROM ELIGIBILITY_ACTIVITIES WHERE ID='".$_REQUEST['activity_id']."'"));
					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Activity:').' </b></font>'.$activity[1]['TITLE'].'<BR>';
				}
				if($_REQUEST['search_modfunc']=='search' || !$_REQUEST['search_modfunc'])
					$activities_RET = DBGet(DBQuery("SELECT ID,TITLE FROM ELIGIBILITY_ACTIVITIES WHERE SCHOOL_ID='".UserSchool()."' AND SYEAR='".UserSyear()."'"));
				$select = "<SELECT name=activity_id><OPTION value=''>"._('N/A')."</OPTION>";
				if(count($activities_RET))
				{
					foreach($activities_RET as $activity)
						$select .= "<OPTION value=$activity[ID]>$activity[TITLE]</OPTION>";
				}
				$select .= '</SELECT>';
				if(AllowUse('Eligibility/Student.php') || AllowUse('Eligibility/AddActivity.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Activity')."</TD><TD>".$select."</TD></TR>";
			break;		
			
			case 'mailing_labels':
				if($_REQUEST['mailing_labels']=='Y')
				{
					$extra['SELECT'] .= ',sam.ADDRESS_ID,sam.ADDRESS_ID AS MAILING_LABEL';
					$extra['FROM'] = " LEFT OUTER JOIN STUDENTS_JOIN_ADDRESS sam ON (sam.STUDENT_ID=ssm.STUDENT_ID AND sam.MAILING='Y') ".$extra['FROM'];
					$extra['functions'] += array('MAILING_LABEL'=>'MailingLabel');
				}
				
				$extra['second_col'] .= '<BR><TABLE><TR><TD width=100> &nbsp; </TD><TD><INPUT type=checkbox name=mailing_labels value=Y></TD><TD>'._('Mailing Labels').'</TD></TR></TABLE>';
			break;

			case 'document_template':
				$templates_RET = DBGet(DBQuery("SELECT ID,TITLE FROM HEADER_TEMPLATES ORDER BY TITLE"));
				foreach($templates_RET as $template)
					$options .= "<OPTION value=$template[ID]>".$template['TITLE'].'</OPTION>';
				$extra['second_col'] .= '<TABLE style="display:inline;"><TR><TD width=100> &nbsp; </TD><TD align='.ALIGN_RIGHT.'>'._('Document Template').' </TD><TD><SELECT name=_template_id><OPTION value="">'._('None').'</OPTION>'.$options.'</SELECT></TD></TR></TABLE>';
			break;
			
			case 'balance':
				if(is_numeric($_REQUEST['balance_low']) && is_numeric($_REQUEST['balance_high']))
				{
					if($_REQUEST['balance_low'] > $_REQUEST['balance_high'])
					{
						$temp = $_REQUEST['balance_high'];
						$_REQUEST['balance_high'] = $_REQUEST['balance_low'];
						$_REQUEST['balance_low'] = $temp;
					}
					$extra['WHERE'] .= " AND (COALESCE((SELECT SUM(f.AMOUNT) FROM BILLING_FEES f WHERE f.STUDENT_ID=ssm.STUDENT_ID AND f.SYEAR=ssm.SYEAR),0)-COALESCE((SELECT SUM(p.AMOUNT) FROM BILLING_PAYMENTS p WHERE p.STUDENT_ID=ssm.STUDENT_ID AND p.SYEAR=ssm.SYEAR),0)) BETWEEN '$_REQUEST[balance_low]' AND '$_REQUEST[balance_high]' ";
				}
				if(AllowUse('Billing/StudentBalances.php'))
					$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Billing Balance')."<BR></TD><TD><small>"._('Between')." </small><INPUT type=text name=balance_low size=5 maxlength=10> &amp; <INPUT type=text name=balance_high size=5 maxlength=10></TD></TR>";
			break;
			
			case 'discipline':
				if(is_numeric($_REQUEST['referrals_low']) && is_numeric($_REQUEST['referrals_high']))
				{
					if($_REQUEST['referrals_low'] > $_REQUEST['referrals_high'])
					{
						$temp = $_REQUEST['referrals_low'];
						$_REQUEST['referrals_low'] = $_REQUEST['referrals_high'];
						$_REQUEST['referrals_low'] = $temp;
					}
		
					if($_REQUEST['referrals_low']==$_REQUEST['referrals_high'])
						$extra['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM DISCIPLINE_REFERRALS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SYEAR=ssm.SYEAR) = '$_REQUEST[referrals_low]'";
					else
						$extra['WHERE'] .= " AND (SELECT COUNT(*) AS COUNT FROM DISCIPLINE_REFERRALS d WHERE ssm.STUDENT_ID=d.STUDENT_ID AND d.SYEAR=ssm.SYEAR) BETWEEN '$_REQUEST[referrals_low]' AND '$_REQUEST[referrals_high]'";

					$_FOCUS['SearchTerms'] .= '<font color=gray><b>'._('Number of Referrals received ')._(' between').': </b></font>'.$_REQUEST['referrals_low'].' &amp; '.$_REQUEST['referrals_high'].'<BR>';
				}
				$extra['search'] .= "<TR><TD align=".ALIGN_RIGHT." width=150>"._('Number of Referrals recieved')."</TD><TD><small>"._('Between')." </small> <INPUT type=text name=referrals_low size=3 maxlength=5> <small>&amp;</small> <INPUT type=text name=referrals_high size=3 maxlength=5></TD></TR>";

				if(is_array($_REQUEST['discipline']))
				{
					foreach($_REQUEST['discipline'] as $key=>$value)
					{
						if(!$value)
							unset($_REQUEST['discipline'][$key]);
					}
				}
				if($_REQUEST['month_discipline_entry_begin'] && $_REQUEST['day_discipline_entry_begin'] && $_REQUEST['year_discipline_entry_begin'])
				{
					$_REQUEST['discipline_entry_begin'] = $_REQUEST['day_discipline_entry_begin'].'-'.$_REQUEST['month_discipline_entry_begin'].'-'.$_REQUEST['year_discipline_entry_begin'];
					if(!VerifyDate($_REQUEST['discipline_entry_begin']))
						unset($_REQUEST['discipline_entry_begin']);
					$_REQUEST['discipline_entry_end'] = $_REQUEST['day_discipline_entry_end'].'-'.$_REQUEST['month_discipline_entry_end'].'-'.$_REQUEST['year_discipline_entry_end'];
					if(!VerifyDate($_REQUEST['discipline_entry_end']))
						unset($_REQUEST['discipline_entry_end']);
				}
				if($_REQUEST['discipline_begin'] && $_REQUEST['discipline_end'])
				{
					foreach($_REQUEST['discipline_begin'] as $key=>$begin)
					{
						if($begin > $_REQUEST['discipline_end'][$key])
						{
							$temp = $_REQUEST['discipline_begin'][$key];
							$_REQUEST['discipline_begin'][$key] = $_REQUEST['discipline_end'][$key];
							$_REQUEST['discipline_end'][$key] = $temp;
						}
					}
				}
				if($_REQUEST['discipline_reporter'] || ($_REQUEST['discipline_entry_begin'] && $_REQUEST['discipline_entry_end']) || count($_REQUEST['discipline']))
				{
					$extra['WHERE'] .= ' AND dr.STUDENT_ID=ssm.STUDENT_ID AND dr.SYEAR=ssm.SYEAR AND dr.SCHOOL_ID=ssm.SCHOOL_ID ';
					$extra['FROM'] .= ',DISCIPLINE_REFERRALS dr ';			
					if($_REQUEST['discipline_reporter'])
						$extra['WHERE'] .= " AND dr.STAFF_ID='$_REQUEST[discipline_reporter]' ";
					if($_REQUEST['discipline_entry_begin'] && $_REQUEST['discipline_entry_end'])
						$extra['WHERE'] .= " AND dr.ENTRY_DATE BETWEEN '$_REQUEST[discipline_entry_begin]' AND '$_REQUEST[discipline_entry_end]' ";
				}
				$extra['search'] .= '<TR><TD align='.ALIGN_RIGHT.' width=150>'._('Reporter').'</TD><TD>';
				$users_RET = DBGet(DBQuery("SELECT STAFF_ID,FIRST_NAME,LAST_NAME,MIDDLE_NAME FROM USERS WHERE SYEAR='".UserSyear()."' AND SCHOOLS LIKE '%,".UserSchool().",%' AND PROFILE IN ('admin','teacher') ORDER BY LAST_NAME,FIRST_NAME,MIDDLE_NAME"));
				$extra['search'] .= '<SELECT name=discipline_reporter><OPTION value="">'._('N/A').'</OPTION>';
				foreach($users_RET as $user)
					$extra['search'] .= '<OPTION value='.$user['STAFF_ID'].'>'.$user['LAST_NAME'].', '.$user['FIRST_NAME'].' '.$user['MIDDLE_NAME'].'</OPTION>';
				$extra['search'] .= '</SELECT>';
				$extra['search'] .= '</TD></TR>';

				$extra['search'] .= '<TR><TD colspan=2>'._('Incident Date').'<BR> &nbsp; &nbsp; <small>'._('Between').' </small> '.PrepareDate('','_discipline_entry_begin',true,array('short'=>true)).' <small>&</small> '.PrepareDate('','_discipline_entry_end',true,array('short'=>true))."</font></TD></TR>";
				$categories_RET = DBGet(DBQuery("SELECT f.ID,u.TITLE,f.DATA_TYPE,u.SELECT_OPTIONS FROM DISCIPLINE_FIELDS f,DISCIPLINE_FIELD_USAGE u WHERE u.DISCIPLINE_FIELD_ID=f.ID AND u.SYEAR='".UserSyear()."' AND u.SCHOOL_ID='".UserSchool()."' AND f.DATA_TYPE!='textarea'"));
				foreach($categories_RET as $category)
				{
					if($category['DATA_TYPE']!='date')
					{
						$extra['search'] .= '<TR><TD align='.ALIGN_RIGHT.' width=150>'.$category['TITLE'].'</TD><TD>';
						switch($category['DATA_TYPE'])
						{
							case 'text':
								$extra['search'] .= '<INPUT type=text name=discipline['.$category['ID'].']>';
								if($_REQUEST['discipline'][$cateogory['ID']])
									$extra['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." LIKE '".$_REQUEST['discipline'][$cateogory['ID']]."%' ";
							break;
							case 'checkbox':
								$extra['search'] .= '<INPUT type=checkbox name=discipline['.$category['ID'].'] value=Y>';
								if($_REQUEST['discipline'][$cateogory['ID']])
									$extra['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." = 'Y' ";
							break;
							case 'numeric':
								$extra['search'] .= '<small>'._('Between').' </small><INPUT type=text name=discipline_begin['.$category['ID'].'] size=3 maxlength=11> & <INPUT type=text name=discipline_end['.$category['ID'].'] size=3 maxlength=11>';
								if($_REQUEST['discipline_begin'][$cateogory['ID']] && $_REQUEST['discipline_begin'][$cateogory['ID']])
									$extra['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." BETWEEN '".$_REQUEST['discipline_begin'][$cateogory['ID']]."' AND '".$_REQUEST['discipline_end'][$cateogory['ID']]."' ";
							break;
							case 'multiple_checkbox':
							case 'multiple_radio':
							case 'select':
								$category['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$category['SELECT_OPTIONS']));
								$category['SELECT_OPTIONS'] = explode("\r",$category['SELECT_OPTIONS']);
								
								$extra['search'] .= '<SELECT name=discipline['.$category['ID'].']><OPTION value="">'._('N/A').'</OPTION>';
								foreach($category['SELECT_OPTIONS'] as $option)
									$extra['search'] .= '<OPTION value="'.$option.'">'.$option.'</OPTION>';
								$extra['search'] .= '</SELECT>';
								if(($category['DATA_TYPE']=='multiple_radio' || $category['DATA_TYPE']=='select') && $_REQUEST['discipline'][$category['ID']])
									$extra['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." = '".$_REQUEST['discipline'][$category['ID']]."' ";
								elseif($category['DATA_TYPE']=='multiple_checkbox' && $_REQUEST['discipline'][$category['ID']])
									$extra['WHERE'] .= " AND dr.CATEGORY_".$category['ID']." LIKE '%||".$_REQUEST['discipline'][$category['ID']]."||%' ";
							break;
						}
						$extra['search'] .= '</TD></TR>';
					}
				}
			break;
		}
	}
}


?>